package golf.stockinfo.testutil;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class FindStocksPerformance {

	public static void main(String[] args){

		try {
			
			File folder = new File("/Users/golf/Documents/temp/stat");
			File[] files = folder.listFiles();
			
			for(File file:files){
				
				FileInputStream fstream = new FileInputStream(file);
				DataInputStream in = new DataInputStream(fstream);
				BufferedReader br = new BufferedReader(new InputStreamReader(in));
				String filename = file.getName().replaceAll("\\.txt", "");
				String strLine;
				float close = 0;
				float buyPrice = 0;
				String date;

				int i = 0;
				// Read File Line By Line
				while ((strLine = br.readLine()) != null) {
					if(i>0){
						
						while(strLine.indexOf("  ") > 0){
							strLine = strLine.replaceAll("  ", " ");
						}
						String[] lineItems = strLine.split(" ");
						date = lineItems[0];
						close = Float.parseFloat(lineItems[4].replaceAll(",",""));
						
						if(i == 2){
							buyPrice = close;
							System.out.print(filename + ";" + date + ";" + close);
						}
						
					}
					i++;
				}
				
				float percentageGain = (close - buyPrice)/buyPrice*100;
				System.out.println(";" + close + ";" + percentageGain);
				// Close the input stream
				in.close();

			}
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
			e.printStackTrace();

		}
	}
}
